package driver;

import jist.runtime.JistAPI;
import jist.swans.Constants;
import jist.swans.field.Fading;
import jist.swans.field.Field;
import jist.swans.field.Mobility;
import jist.swans.field.PathLoss;
import jist.swans.field.Placement;
import jist.swans.field.Spatial;
import jist.swans.misc.Location;
import jist.swans.misc.Mapper;
import jist.swans.misc.Util;
import jist.swans.net.PacketLoss;
import jist.swans.radio.RadioInfo;

public class testrange {
	
	  public static Field createSim(int nodes, int length)
	  {
	    Location.Location2D bounds = new Location.Location2D(length, length);
	    Placement placement = new Placement.Random(bounds);
	    Mobility mobility = new Mobility.RandomWaypoint(bounds, PAUSE_TIME, GRANULARITY, MAX_SPEED, MIN_SPEED);
	    Spatial spatial = new Spatial.HierGrid(bounds, 5);
	    Fading fading = new Fading.None();
	    PathLoss pathloss = new PathLoss.FreeSpace();
	    Field field = new Field(spatial, fading, pathloss, mobility, Constants.PROPAGATION_LIMIT_DEFAULT);

	    RadioInfo.RadioInfoShared radioInfoShared = RadioInfo.createShared(
	        Constants.FREQUENCY_DEFAULT, Constants.BANDWIDTH_DEFAULT,
	        Constants.TRANSMIT_DEFAULT, Constants.GAIN_DEFAULT,
	        Util.fromDB(Constants.SENSITIVITY_DEFAULT), Util.fromDB(Constants.THRESHOLD_DEFAULT),
	        Constants.TEMPERATURE_DEFAULT, Constants.TEMPERATURE_FACTOR_DEFAULT, Constants.AMBIENT_NOISE_DEFAULT);

	    Mapper protMap = new Mapper(Constants.NET_PROTOCOL_MAX);
	    protMap.mapToNext(Constants.NET_PROTOCOL_HEARTBEAT);
	    PacketLoss pl = new PacketLoss.Zero();

	    for(int i=0; i<nodes; i++)
	    {
	      createNode(i, field, placement, radioInfoShared, protMap, pl, pl);
	    }

	    return field;
	  }
	
	  public static void main(String[] args)
	  {
	  

	    int nodes = 2;
	    int length = 100;
	    int time = 5;
	    float density = nodes / (float)(length/1000.0 * length/1000.0);

	    System.out.println("nodes   = "+nodes);
	    System.out.println("size    = "+length+" x "+length);
	    System.out.println("time    = "+time+" seconds");
	    System.out.print("Creating simulation nodes... ");
	    Field f = createSim(nodes, length);
	    
	    System.out.println("done.");

	    System.out.println("Average density = "+f.computeDensity()*1000*1000+"/km^2");
	    System.out.println("Average sensing = "+f.computeAvgConnectivity(true));
	    System.out.println("Average receive = "+f.computeAvgConnectivity(false));
	    JistAPI.endAt(time*Constants.SECOND);
	  }

}
